package com.lmp.ui;

import android.os.Bundle;
import android.os.Environment;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

import org.apache.http.util.EncodingUtils;

import com.example.liftui.R;

import android.app.Activity;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.widget.AbsoluteLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;

public class MainActivity extends Activity {	
	 UiParams mUiParams;
	 AbsoluteLayout mLayout;  

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState); 
        
//		requestWindowFeature(Window.FEATURE_NO_TITLE);  //无title  
//		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,  
//		              WindowManager.LayoutParams.FLAG_FULLSCREEN);  //全屏    
        
        mLayout = (AbsoluteLayout) new AbsoluteLayout(this);
        
        View decorView = getWindow().getDecorView();
		int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN;  
		decorView.setSystemUiVisibility(uiOptions);
		
        setContentView(mLayout);   
        mUiParams = new UiParams(this);              
		try {
        	mUiParams.getDisplayMetrics();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		mUiParams.initLandLayoutParams();
       
		try {
			InitUI();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
    
    public void InitUI() throws Exception {
    	if(mUiParams.GUI_LCD_XMAX>0 && mUiParams.GUI_LCD_YMAX>0)
    	{    		 
    		AbsoluteLayout.LayoutParams G_REG_LCDLayoutParams = new AbsoluteLayout.LayoutParams(mUiParams.GUI_LCD_XMAX, mUiParams.GUI_LCD_YMAX, 0, 0);
    		ImageView LCDtextView = new ImageView(this);       
	        LCDtextView.setImageBitmap(getLoacalBitmap("Fixedbkgnd.png")); 
	        LCDtextView.setScaleType(ImageView.ScaleType.FIT_XY);
			//DIRtextView.setId(1);//设置View id (AbsoluteLayout不需要)
	        LCDtextView.setLayoutParams(G_REG_LCDLayoutParams);//设置布局参数
		    mLayout.addView(LCDtextView);//AbsoluteLayout添加子View
    	}
    	
    	if(mUiParams.G_REG_DIR.x_len>0 && mUiParams.G_REG_DIR.y_len>0)
    	{    		 
    		AbsoluteLayout.LayoutParams G_REG_DIRLayoutParams = new AbsoluteLayout.LayoutParams(mUiParams.G_REG_DIR.x_len, mUiParams.G_REG_DIR.y_len, mUiParams.G_REG_DIR.s_x, mUiParams.G_REG_DIR.s_y);
    		ImageView DIRtextView = new ImageView(this);       
			DIRtextView.setImageBitmap(getLoacalBitmap("DIR_U0.png")); 
			DIRtextView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
			//DIRtextView.setId(1);//设置View id (AbsoluteLayout不需要)
			DIRtextView.setLayoutParams(G_REG_DIRLayoutParams);//设置布局参数
		    mLayout.addView(DIRtextView);//AbsoluteLayout添加子View
    	}
	    
    	if(mUiParams.G_REG_FLR.x_len>0 && mUiParams.G_REG_FLR.y_len>0)
    	{
    		AbsoluteLayout.LayoutParams G_REG_FLRLayoutParams = new AbsoluteLayout.LayoutParams(mUiParams.G_REG_FLR.x_len, mUiParams.G_REG_FLR.y_len, mUiParams.G_REG_FLR.s_x, mUiParams.G_REG_FLR.s_y);
    		LinearLayout G_REG_FLRLayoutsub = new LinearLayout(this);
    		LinearLayout.LayoutParams G_REG_FLRLayoutParamssub = new LinearLayout.LayoutParams(mUiParams.G_REG_FLR.x_len/2,mUiParams.G_REG_FLR.y_len);
    		
    		G_REG_FLRLayoutParamssub.gravity = Gravity.CENTER;

    		ImageView FLRtextView = new ImageView(this);       
			FLRtextView.setImageBitmap(getLoacalBitmap("0x42.png")); 
			FLRtextView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
			G_REG_FLRLayoutsub.addView(FLRtextView, G_REG_FLRLayoutParamssub);
			
    		ImageView FLRtextView1 = new ImageView(this);       
			FLRtextView1.setImageBitmap(getLoacalBitmap("0x42.png")); 
			FLRtextView1.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
			G_REG_FLRLayoutsub.addView(FLRtextView1, G_REG_FLRLayoutParamssub);
			
//			ImageView FLRtextView2 = new ImageView(this);       
//			FLRtextView2.setImageBitmap(getLoacalBitmap("0x32.png")); 
//			FLRtextView2.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
//			G_REG_FLRLayoutsub.addView(FLRtextView2, G_REG_FLRLayoutParamssub);

		    mLayout.addView(G_REG_FLRLayoutsub, G_REG_FLRLayoutParams);
    	}
	    
    	if(mUiParams.G_REG_TIME.x_len>0 && mUiParams.G_REG_TIME.y_len>0)
    	{
    		AbsoluteLayout.LayoutParams G_REG_TIMELayoutParams = new AbsoluteLayout.LayoutParams(mUiParams.G_REG_TIME.x_len, mUiParams.G_REG_TIME.y_len, mUiParams.G_REG_TIME.s_x, mUiParams.G_REG_TIME.s_y);
			TextView TIMEtextView = new TextView(this);       
			TIMEtextView.setText("TIME View"); 
			TIMEtextView.setGravity(Gravity.CENTER);
		    mLayout.addView(TIMEtextView, G_REG_TIMELayoutParams);
    	}

    	if(mUiParams.G_REG_DAY.x_len>0 && mUiParams.G_REG_DAY.y_len>0)
    	{
    		AbsoluteLayout.LayoutParams G_REG_DAYLayoutParams = new AbsoluteLayout.LayoutParams(mUiParams.G_REG_DAY.x_len, mUiParams.G_REG_DAY.y_len, mUiParams.G_REG_DAY.s_x, mUiParams.G_REG_DAY.s_y);
    		LinearLayout G_REG_DAYLayoutsub = new LinearLayout(this);
    		LinearLayout.LayoutParams G_REG_DAYLayoutParamssub = new LinearLayout.LayoutParams(mUiParams.G_REG_DAY.x_len/10,mUiParams.G_REG_FLR.y_len);
    		
    		G_REG_DAYLayoutParamssub.gravity = Gravity.CENTER;
    		
    		ImageView DAYtextView = new ImageView(this);       
    		DAYtextView.setImageBitmap(getLoacalBitmap("0x32.png")); 
    		DAYtextView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
    		G_REG_DAYLayoutsub.addView(DAYtextView, G_REG_DAYLayoutParamssub);
			
    		ImageView DAYtextView1 = new ImageView(this);       
    		DAYtextView1.setImageBitmap(getLoacalBitmap("0x30.png")); 
    		DAYtextView1.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
    		G_REG_DAYLayoutsub.addView(DAYtextView1, G_REG_DAYLayoutParamssub);
    		
    		ImageView DAYtextView2 = new ImageView(this);       
    		DAYtextView2.setImageBitmap(getLoacalBitmap("0x31.png")); 
    		DAYtextView2.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
    		G_REG_DAYLayoutsub.addView(DAYtextView2, G_REG_DAYLayoutParamssub);
    		
    		ImageView DAYtextView3 = new ImageView(this);       
    		DAYtextView3.setImageBitmap(getLoacalBitmap("0x36.png")); 
    		DAYtextView3.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
    		G_REG_DAYLayoutsub.addView(DAYtextView3, G_REG_DAYLayoutParamssub);
    		
    		ImageView DAYtextView4 = new ImageView(this);       
    		DAYtextView4.setImageBitmap(getLoacalBitmap("0x2e.png")); 
    		DAYtextView4.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
    		G_REG_DAYLayoutsub.addView(DAYtextView4, G_REG_DAYLayoutParamssub);
    		
    		ImageView DAYtextView5 = new ImageView(this);       
    		DAYtextView5.setImageBitmap(getLoacalBitmap("0x30.png")); 
    		DAYtextView5.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
    		G_REG_DAYLayoutsub.addView(DAYtextView5, G_REG_DAYLayoutParamssub);
    		
    		ImageView DAYtextView6 = new ImageView(this);       
    		DAYtextView6.setImageBitmap(getLoacalBitmap("0x31.png")); 
    		DAYtextView6.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
    		G_REG_DAYLayoutsub.addView(DAYtextView6, G_REG_DAYLayoutParamssub);
    		
    		ImageView DAYtextView7 = new ImageView(this);       
    		DAYtextView7.setImageBitmap(getLoacalBitmap("0x2e.png")); 
    		DAYtextView7.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
    		G_REG_DAYLayoutsub.addView(DAYtextView7, G_REG_DAYLayoutParamssub);
    		
    		ImageView DAYtextView8 = new ImageView(this);       
    		DAYtextView8.setImageBitmap(getLoacalBitmap("0x31.png")); 
    		DAYtextView8.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
    		G_REG_DAYLayoutsub.addView(DAYtextView8, G_REG_DAYLayoutParamssub);
    		
    		ImageView DAYtextView9 = new ImageView(this);       
    		DAYtextView9.setImageBitmap(getLoacalBitmap("0x32.png")); 
    		DAYtextView9.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
    		G_REG_DAYLayoutsub.addView(DAYtextView9, G_REG_DAYLayoutParamssub);
			
		    mLayout.addView(G_REG_DAYLayoutsub, G_REG_DAYLayoutParams);
    	}

    	if(mUiParams.G_REG_WEEK.x_len>0 && mUiParams.G_REG_WEEK.y_len>0)
    	{
    		AbsoluteLayout.LayoutParams G_REG_WEEKLayoutParams = new AbsoluteLayout.LayoutParams(mUiParams.G_REG_WEEK.x_len, mUiParams.G_REG_WEEK.y_len, mUiParams.G_REG_WEEK.s_x, mUiParams.G_REG_WEEK.s_y);
    		ImageView WEEKtextView = new ImageView(this);       
			WEEKtextView.setImageBitmap(getLoacalBitmap("Monday.png"));  
			WEEKtextView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
		    mLayout.addView(WEEKtextView, G_REG_WEEKLayoutParams);
    	}

    	if(mUiParams.G_REG_ALARM.x_len>0 && mUiParams.G_REG_ALARM.y_len>0)
    	{
    		AbsoluteLayout.LayoutParams G_REG_ALARMLayoutParams = new AbsoluteLayout.LayoutParams(mUiParams.G_REG_ALARM.x_len, mUiParams.G_REG_ALARM.y_len, mUiParams.G_REG_ALARM.s_x, mUiParams.G_REG_ALARM.s_y);
    		ImageView ALARMtextView = new ImageView(this);       
			ALARMtextView.setImageBitmap(getLoacalBitmap("Fire_0.png"));  
			ALARMtextView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
		    mLayout.addView(ALARMtextView, G_REG_ALARMLayoutParams);
    	}

    	if(mUiParams.G_REG_BKGND.x_len>0 && mUiParams.G_REG_BKGND.y_len>0)
    	{
    		AbsoluteLayout.LayoutParams G_REG_BKGNDLayoutParams = new AbsoluteLayout.LayoutParams(mUiParams.G_REG_BKGND.x_len, mUiParams.G_REG_BKGND.y_len, mUiParams.G_REG_BKGND.s_x, mUiParams.G_REG_BKGND.s_y);
			TextView BKGNDtextView = new TextView(this);       
			BKGNDtextView.setText("BKGND View"); 
		    mLayout.addView(BKGNDtextView, G_REG_BKGNDLayoutParams);
    	}

    	if(mUiParams.G_REG_LOGO.x_len>0 && mUiParams.G_REG_LOGO.y_len>0)
    	{
    		AbsoluteLayout.LayoutParams G_REG_LOGOLayoutParams = new AbsoluteLayout.LayoutParams(mUiParams.G_REG_LOGO.x_len, mUiParams.G_REG_LOGO.y_len, mUiParams.G_REG_LOGO.s_x, mUiParams.G_REG_LOGO.s_y);
			TextView LOGOtextView = new TextView(this);       
			LOGOtextView.setText("LOGO View"); 
		    mLayout.addView(LOGOtextView, G_REG_LOGOLayoutParams);
    	}

    	if(mUiParams.G_REG_OTHER.x_len>0 && mUiParams.G_REG_OTHER.y_len>0)
    	{
    		AbsoluteLayout.LayoutParams G_REG_OTHERLayoutParams = new AbsoluteLayout.LayoutParams(mUiParams.G_REG_OTHER.x_len, mUiParams.G_REG_OTHER.y_len, mUiParams.G_REG_OTHER.s_x, mUiParams.G_REG_OTHER.s_y);
    		ImageView OTHERtextView = new ImageView(this);       
    		OTHERtextView.setImageBitmap(getLoacalBitmap("d0-1.png"));  
    		OTHERtextView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);		
		    mLayout.addView(OTHERtextView, G_REG_OTHERLayoutParams);
    	}

    	if(mUiParams.G_REG_VIDEO.x_len>0 && mUiParams.G_REG_VIDEO.y_len>0)
    	{
    		AbsoluteLayout.LayoutParams G_REG_VIDEOLayoutParams = new AbsoluteLayout.LayoutParams(mUiParams.G_REG_VIDEO.x_len, mUiParams.G_REG_VIDEO.y_len, mUiParams.G_REG_VIDEO.s_x, mUiParams.G_REG_VIDEO.s_y);
			TextView VIDEOtextView = new TextView(this);       
			VIDEOtextView.setText("VIDEO View"); 
			VIDEOtextView.setGravity(Gravity.CENTER);
			VIDEOtextView.setTextColor(Color.RED);
		    mLayout.addView(VIDEOtextView, G_REG_VIDEOLayoutParams);
    	}

    	if(mUiParams.G_REG_TXT.x_len>0 && mUiParams.G_REG_TXT.y_len>0)
    	{
    		AbsoluteLayout.LayoutParams G_REG_TXTLayoutParams = new AbsoluteLayout.LayoutParams(mUiParams.G_REG_TXT.x_len, mUiParams.G_REG_TXT.y_len, mUiParams.G_REG_TXT.s_x, mUiParams.G_REG_TXT.s_y);
			TextView TXTtextView = new TextView(this);       
			TXTtextView.setText(getLoacalText("ad.txt"));
			TXTtextView.setGravity(Gravity.LEFT);
			TXTtextView.setTextColor(Color.BLACK);
		    mLayout.addView(TXTtextView, G_REG_TXTLayoutParams);
    	}
    	
    }
    
    public Bitmap getLoacalBitmap(String url) throws Exception {
    	//String PicPath = Environment.getExternalStorageDirectory()+ "/" + url;
    	String PicPath = "LIFT/" + url;
        try {
             //FileInputStream fis = new FileInputStream(PicPath);
        	 InputStream fis = getAssets().open(PicPath);
             return BitmapFactory.decodeStream(fis);
        } catch (FileNotFoundException e) {
             e.printStackTrace();
             return null;
        }
   }
    
    public String getLoacalText(String url) throws Exception {
    	//String PicPath = Environment.getExternalStorageDirectory()+ "/" + url;
    	String PicPath = "LIFT/" + url;
    	String result = "";
        try {
             //FileInputStream fis = new FileInputStream(PicPath);
        	 InputStream fis = getAssets().open(PicPath);
        	 int length = fis.available();
        	 byte[] buffer = new byte[length];
        	 fis.read(buffer);
        	 result = EncodingUtils.getString(buffer, "GB2312");//1.UTF-8 2.gb2312 3.BIG5
        	 
             return result;
        } catch (FileNotFoundException e) {
             e.printStackTrace();
             return result;
        }
   }
    
}
